package vip.xiaonuo.invoice.core.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import vip.xiaonuo.invoice.modular.system.sellerinfo.entity.IvoSellerInfo;
import vip.xiaonuo.invoice.modular.system.sellerinfo.service.IvoSellerInfoService;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class BizEventListener {
    @Autowired
    IvoSellerInfoService ivoSellerInfoService;

    // 使用静态 ConcurrentHashMap 确保单例且线程安全
    private static final Map<String, Map<String, String>> SELLER_INFO_CACHE = new ConcurrentHashMap<>();

    @EventListener(ApplicationStartedEvent.class)
    public void onApplicationStarted(ApplicationStartedEvent event) {
        log.info("开始执行项目启动任务：查询所有sellerInfo");
        try {
            List<IvoSellerInfo> sellerInfoList = ivoSellerInfoService.list();
            log.info("查询到的sellerInfo数量：{}", sellerInfoList.size());

            // 清空缓存并重新加载数据
            SELLER_INFO_CACHE.clear();
            for (IvoSellerInfo sellerInfo : sellerInfoList) {
                // 创建商户信息的内部map
                Map<String, String> sellerDetails = new ConcurrentHashMap<>();

                // 存储需要的字段
                sellerDetails.put("loginAccount", sellerInfo.getLoginAccount());
                sellerDetails.put("appId", sellerInfo.getAppId());
                sellerDetails.put("appSecret", sellerInfo.getAppSecret());
                sellerDetails.put("sellerTaxNumber", sellerInfo.getSellerTaxNumber());
                // 可以根据实际需要添加更多字段

                // 以商户ID为key存入外层map
                SELLER_INFO_CACHE.put(sellerInfo.getSellerId(), sellerDetails);
            }

            log.info("sellerInfo缓存初始化完成，共缓存 {} 条商户信息", SELLER_INFO_CACHE.size());

            // 打印缓存的详细信息
            printCacheInfo();
        } catch (Exception e) {
            log.error("sellerInfo缓存初始化异常：", e);
        }
    }

    // 打印缓存信息的方法
    private void printCacheInfo() {
        log.info("===== 开始打印缓存信息 =====");

        if (SELLER_INFO_CACHE.isEmpty()) {
            log.info("缓存为空");
            return;
        }

        // 遍历缓存并打印每个商户的信息
        for (Map.Entry<String, Map<String, String>> entry : SELLER_INFO_CACHE.entrySet()) {
            String sellerId = entry.getKey();
            Map<String, String> sellerDetails = entry.getValue();

            log.info("商户ID: {}", sellerId);
            // 打印商户详情
            for (Map.Entry<String, String> detailEntry : sellerDetails.entrySet()) {
                // 注意：为了安全起见，敏感信息如appSecret可以部分隐藏
                String key = detailEntry.getKey();
                String value = detailEntry.getValue();

                if ("appSecret".equals(key) && value != null && value.length() > 6) {
                    // 隐藏appSecret的中间部分，只显示前3位和后3位
                    value = value.substring(0, 3) + "****" + value.substring(value.length() - 3);
                }

                log.info("  {}: {}", key, value);
            }
        }

        log.info("===== 缓存信息打印完成 =====");
    }

    // 提供静态方法供其他地方获取缓存数据
    public static Map<String, Map<String, String>> getSellerInfoCache() {
        return SELLER_INFO_CACHE;
    }

    // 提供根据商户ID获取特定商户信息的方法
    public static Map<String, String> getSellerInfoById(String sellerId) {
        return SELLER_INFO_CACHE.get(sellerId);
    }
}
